package com.vpen.picupload.service.impl;

import com.vpen.picupload.entity.PicUpload;
import com.vpen.picupload.dao.PicUploadDao;
import com.vpen.picupload.service.PicUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * (PicUpload)表服务实现类
 *
 * @author vpen
 * @since 2021-07-31 23:11:10
 */
@Slf4j
@Service("picUploadService")
public class PicUploadServiceImpl implements PicUploadService {
    @Resource
    private PicUploadDao picUploadDao;
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PicUpload queryById(Object id) {
        return this.picUploadDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<PicUpload> queryAllByLimit(int offset, int limit) {
        return this.picUploadDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param picUpload 实例对象
     * @return 实例对象
     */
    @Override
    public PicUpload insert(PicUpload picUpload) {
        this.picUploadDao.insert(picUpload);
        return picUpload;
    }

    /**
     * 修改数据
     *
     * @param picUpload 实例对象
     * @return 实例对象
     */
    @Override
    public PicUpload update(PicUpload picUpload) {
        this.picUploadDao.update(picUpload);
        return this.queryById(picUpload.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Object id) {
        return this.picUploadDao.deleteById(id) > 0;
    }

    /**
     * 作者:韦鹏
     * 描述:生成url
     * */
    @Override
    public boolean upLoad(MultipartFile file, PicUpload picUpload, HttpServletRequest request) {

        String randomName = UUID.randomUUID().toString().replace("-", "");
        String oldName = file.getOriginalFilename();
        // 获取名字的后缀
        String extension = FilenameUtils.getExtension(oldName);

        String newName = randomName+"."+extension;

        File imgDir = new File(uploadFolder);

        // 存图片的静态目录是否存在，不在创建
        if (!imgDir.exists()){
            // 如果有多级目录就会一个个创建
            boolean b = imgDir.mkdirs();
            if (!b) {
                log.info("文件夹创建失败");
            }
        }

        try {
            // 生成到 File中
            file.transferTo(new File(uploadFolder,newName));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +"/images/"+newName;
        log.info("url===>{}",url);
        picUpload.setPicUrl(url);
        int i = picUploadDao.insert(picUpload);
        return i != 0;
    }
}